package com.tmzn.service;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.tmzn.entity.param.DeviceParam;
import com.tmzn.openfeign.MsgService;
import com.tmzn.transdata.DataParam;
import com.tmzn.transdata.RpcResult;
import com.tmzn.transdata.entity.EndCharge;
import com.tmzn.transdata.entity.MainBoard;
import com.tmzn.transdata.entity.PlanCharge;
import com.tmzn.transdata.entity.StartCharge;
import com.tmzn.transdata.entity.opertype.OperEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class DeviceControlerService {
    @Autowired
    private MsgService msgService;

    /**
     * 获取主板参数
     * @param deviceId
     * @param ccid
     */
    public void getMainBoard(String deviceId,String ccid){
        DataParam dataParam = new DataParam();
        MainBoard mainBoard = new MainBoard();
        JSONObject object1 = JSONObject.parseObject(JSON.toJSONString(mainBoard));
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId);
        dataParam.setType(OperEnum.GetMainBord.getType());
        msgService.sendMsg(dataParam);
        log.info("发送获取主板的消息，{}",deviceId);
    }

    /**
     * 设置主板参数
     * @param deviceId
     * @param ccid
     * @param mainBoard
     */
    public void setMainBoard(String deviceId,String ccid,MainBoard mainBoard){
        DataParam dataParam = new DataParam();
        JSONObject object1 = JSONObject.parseObject(JSON.toJSONString(mainBoard));
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId);
        dataParam.setType(OperEnum.SetMainBord.getType());
        msgService.sendMsg(dataParam);
        log.info("发送设置主板的消息，{}",deviceId);
    }

    /**
     * 开始充电
     * @param deviceId
     */
    public void startCharge(String deviceId,String ccid,Integer port){
        DataParam dataParam = new DataParam();
        StartCharge mainBoard = new StartCharge();
        mainBoard.setPort(port);
        JSONObject object1 = JSONObject.parseObject(JSON.toJSONString(mainBoard));
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId);
        dataParam.setType(OperEnum.StartCharge.getType());
        msgService.sendMsg(dataParam);
        log.info("发送开始充电的消息，{}",deviceId);
    }

    /**
     * 结束充电
     * @param deviceId
     * @param ccid
     */
    public void stopCharge(String deviceId,String ccid,Integer port){
        DataParam dataParam = new DataParam();
        EndCharge mainBoard = new EndCharge();
        mainBoard.setPort(port);
        JSONObject object1 = JSONObject.parseObject(JSON.toJSONString(mainBoard));
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId);
        dataParam.setType(OperEnum.EndCharge.getType());
        msgService.sendMsg(dataParam);
        log.info("发送停止充电的消息，{}",deviceId);
    }

    /**
     * 重启设备
     * @param deviceId
     * @param ccid
     */
    public void restart(String deviceId,String ccid){
        DataParam dataParam = new DataParam();
        MainBoard mainBoard = new MainBoard();
        JSONObject object1 = JSONObject.parseObject(JSON.toJSONString(mainBoard));
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId);
        dataParam.setType(OperEnum.RestartDevice.getType());
        msgService.sendMsg(dataParam);
        log.info("发送重启设备的消息，{}",deviceId);
    }

    /**
     * 重置设备
     * @param deviceId
     * @param ccid
     */
    public void reset(String deviceId,String ccid){
        DataParam dataParam = new DataParam();
        MainBoard mainBoard = new MainBoard();
        JSONObject object1 = JSONObject.parseObject(JSON.toJSONString(mainBoard));
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId);
        dataParam.setType(OperEnum.ResetDevice.getType());
        msgService.sendMsg(dataParam);

        log.info("发送重置设备的消息，{}",deviceId);
    }


    /**
     * 发送获取端口状态指令
     * @param deviceParam
     */
    public void sendPortDetailCmd(DeviceParam deviceParam) {
        String deviceId = deviceParam.getDeviceId();
        String ccid = deviceParam.getCcid();
        DataParam dataParam = new DataParam();
        JSONObject object1 = new JSONObject();
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId.toString());
        dataParam.setType(OperEnum.PortDetail.getType());
        RpcResult rpcResult = msgService.sendMsg(dataParam);
        log.info("发送获取端口详情的消息，{}",deviceId);
    }

    /**
     * 预约充电
     */
    public void planCharge(String deviceId, String ccid, PlanCharge planCharge) {
        DataParam dataParam = new DataParam();
        JSONObject object1 = JSONObject.parseObject(JSON.toJSONString(planCharge));
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId.toString());
        dataParam.setType(OperEnum.PlanCHarge.getType());
        RpcResult rpcResult = msgService.sendMsg(dataParam);
        log.info("发送预约充电的消息，{}",deviceId);
    }


    /**
     * 取消预约充电
     */
    public void cancelChargePlan(String deviceId, String ccid, int port) {
        DataParam dataParam = new DataParam();
        JSONObject object1 = new JSONObject();
        object1.put("port",port);
        dataParam.setData(object1);
        dataParam.setCcid(ccid);
        dataParam.setDeviceId(deviceId.toString());
        dataParam.setType(OperEnum.CancelPlanCHarge.getType());
        RpcResult rpcResult = msgService.sendMsg(dataParam);
        log.info("发送取消预约充电的消息，{},端口：{}",deviceId,port);
    }


}
